package com.cy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cy.common.entity.Result;
import com.cy.common.exception.CommonException;
import com.cy.common.utils.JwtUtils;
import com.cy.mapper.*;
import com.cy.pojo.*;
import com.cy.pojo.vo.LoginVo;
import com.cy.pojo.vo.UserInfoVo;
import com.cy.service.LoginService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author : 残影
 * @Date: 2022/4/13
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private TokenMapper tokenMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private DepartmentMapper departmentMapper;
    @Autowired
    private AcademyMapper academyMapper;

    @Override
    public Result getLogin(LoginVo userVo) {
        if (StringUtils.isEmpty(userVo.getUsername()) && StringUtils.isEmpty(userVo.getPhoneNumber())) {
            throw new CommonException("账号和密码不能同时为空");
        }
        UsernamePasswordAuthenticationToken authentication;
        if (StringUtils.isNotEmpty(userVo.getUsername())) {
            authentication = new UsernamePasswordAuthenticationToken(userVo.getUsername(), userVo.getPassword());
        } else {
            authentication = new UsernamePasswordAuthenticationToken(userVo.getPhoneNumber(), userVo.getPassword());
        }

        Authentication authenticate = authenticationManager.authenticate(authentication);
        if (ObjectUtils.isEmpty(authenticate)) {
            throw new CommonException("用户名或密码错误");
        }
        LoginUser loginUser = (LoginUser) authenticate.getPrincipal();
        String token = JwtUtils.getJwtToken(loginUser.getUser().getUserId(), loginUser.getUser().getNickName());
        Token tokenInfo = new Token();
        tokenInfo.setId(loginUser.getUser().getUserId());
        tokenInfo.setToken(token);
        Token tokenExist = tokenMapper.selectById(loginUser.getUser().getUserId());
        if (ObjectUtils.isNotEmpty(tokenExist)) {
            tokenMapper.deleteById(loginUser.getUser().getUserId());
        }
        tokenMapper.insert(tokenInfo);
        return Result.ok("登录成功").put("token", token);
    }

    @Override
    public void logout(HttpServletRequest request) {
        Long userId = JwtUtils.getUserIdByRequest(request);
        tokenMapper.deleteById(userId);
    }

    @Override
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = JwtUtils.getUserIdByRequest(request);
        User user = userMapper.selectById(userId);
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(user, userInfoVo);
        Department department = departmentMapper.selectById(user.getDepartmentId());
        userInfoVo.setDepartmentName(department != null ? department.getDepartmentName() : null);
        Academy academy = academyMapper.selectById(user.getAcademyId());
        userInfoVo.setAcademyName(academy != null ? academy.getAcademyName() : null);
        return Result.ok("查询成功").put("user", userInfoVo);
    }
}
